<?php 
	//加入购物车操作
	session_start();
	header("Content-type:text/html;charset=utf-8");
	$conn = 	$conn = mysqli_connect('127.0.0.1','root','pwd','admin',3306);//链接数据库('localhost','hotel','vw8jj2','shop',3308);//链接数据库
	mysqli_set_charset($conn,'utf8'); //设定字符集

	//1.接受传递过来的post参数
	$productid = intval($_POST['productid']);
	$num = intval($_POST['num']);
	//2.准备要添加的购物车数据
	$userid = $_SESSION['admin_name'];

	$sql_select = "select price from shop_product where id=?";
	$stmt=mysqli_prepare($conn,$sql_select); //准备执行一个 SQL 语句
	mysqli_stmt_bind_param($stmt,'s',$productid); //将变量绑定到准备好的语句作为参数
	mysqli_stmt_execute($stmt);//执行准备的陈述
	$result=mysqli_stmt_get_result($stmt);//从准备好的语句获取结果集
	$data=mysqli_fetch_assoc($result);//从结果集中取得一行作为关联数组,用关键字作为索引

	$price = $data['price'];
	$createtime = time();
	//3.完成购物车数据的添加操作(判断该商品呢是否已经在购物车中)
	$sql_insert = "insert into shop_cart(productid,userid,num,price,createtime)values(?,?,?,?,?)";
	$stmt=mysqli_prepare($conn,$sql_insert);

	mysqli_stmt_bind_param($stmt,'sssss',$productid,$userid,$num,$price,$createtime);

	$row=mysqli_stmt_execute($stmt);
	//4.返回最终添加的结果
	if ($row) {
		$response = array(
			'errno' => 0,
			'errmsg' => 'success',
			'data' => true,
		);
	}else{
		$response = array(
			'errno' => -1,
			'errmsg' => 'fail',
			'data' => false,
		);
	}

	echo json_encode($response);
?>